package starter.pageobjects;

import net.serenitybdd.annotations.DefaultUrl;
import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import static java.time.temporal.ChronoUnit.SECONDS;

@DefaultUrl("http://101.34.221.219:8010/")
public class HomePage extends PageObject {
    public static final By USERNAME = By.linkText("chenwenbin");
    public static final By USERLOGINBTN = By.linkText("登录");
    public static final By USERREGBTN = By.linkText("注册");
    public static final By SEARCHBOX = By.id("search-input");
    public static final By SEARCHSUBMITBTN = By.id("ai-topsearch");
    public static final By USERCENTERLINK = By.xpath("//*[@class='menu-hd login-event']");
    public static final By USERLOGINPOPWINDOWIFRAME = By.xpath("//div[@class='am-popup-inner']/iframe");
    public static final By USERNAMEINPUTFIELD_POP = By.name("accounts");
    public static final By USERPASSWORDINPUTFIELD_POP = By.name("pwd");
    public static final By USERLOGINBTN_POP = By.xpath("//button[@type='submit']");


    public void clickOnLogonBtn() {
        find(HomePage.USERLOGINBTN).click();
    }

    public void clickOnSearchBtn() {
        find(HomePage.SEARCHSUBMITBTN).click();
    }

    public void clickOnUserCenterLink() {
        find(HomePage.USERCENTERLINK).click();
    }

    public void switchToLoginFrame() {
        WebElement loginFrame = find(HomePage.USERLOGINPOPWINDOWIFRAME);
        getDriver().switchTo().frame(loginFrame);
    }

    public void usernameInputFieldPop(String username) {
        find(HomePage.USERNAMEINPUTFIELD_POP).type(username);
    }

    public void passwordInputFieldPop(String password) {
        find(HomePage.USERPASSWORDINPUTFIELD_POP).type(password);
    }

    public void clickOnUserLoginBtnPop() {
        withTimeoutOf(5, SECONDS).waitForPresenceOf(HomePage.USERLOGINBTN_POP);
        find(HomePage.USERLOGINBTN_POP).click();
        getDriver().switchTo().defaultContent();
    }
}
